package com.ybxy.web;

import javax.script.ScriptEngine;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class NewsDao {
    public ArrayList<News> findAll() throws SQLException {
        /**
         * nid ntitle nauthor ncreateDate ncontent nsummary
         */
        Connection conn = null;
        Statement statement = null;
        ResultSet result = null;
        ArrayList<News> list = new ArrayList<News>();
        try{
            conn = JDBC.getConn();
            statement = JDBC.getStatement(conn);
            String sql = "select * from news";
            result = JDBC.getResult(sql,statement);
            while(result.next()){
                News news = new News();
                news.setNid(result.getInt("nid"));
                news.setNtid(result.getInt("ntid"));
                news.setNtitle(result.getString("ntitle"));
                news.setNauthor(result.getString("nauthor"));

                news.setNcreateDate(result.getString("ncreateDate"));
                news.setNpicPath(result.getString("npicPath"));
                news.setNcontent(result.getString("ncontent"));
                news.setNmodifyDate(result.getString("nmodifyDate"));
                news.setNsummary(result.getString("nsummary"));
                list.add(news);
            }
            return list;
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            JDBC.close(result,statement,null,conn);
        }
        return null;
    }

    public boolean insert(News news){
        Connection conn = null;
//        Statement statement = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        try{
            conn = JDBC.getConn();
//            statement = JDBC.getStatement(conn);
//            insert into news(ntid,ntitle,nauthor,ncreateDate,npicPath,ncontent,nmodifyDate,nsummary) values(2,"haha","auth","2020-2-3","path","tent","2021-3-1","mmary");
            String sql = "insert into news (ntid,ntitle,nauthor,ncreateDate,npicPath,ncontent,nmodifyDate,nsummary) values (?,?,?,?,?,?,?,?)";
            System.out.println(sql);
            ps = conn.prepareStatement(sql);
            ps.setInt(1,news.getNtid());
            ps.setString(2,news.getNtitle());
            ps.setString(3,news.getNauthor());
            ps.setString(4,news.getNcreateDate());
            ps.setString(5,news.getNpicPath());
            ps.setString(6,news.getNcontent());
            ps.setString(7,news.getNmodifyDate());
            ps.setString(8,news.getNsummary());
            int num = ps.executeUpdate();
            if(num>0){
                return true;
            }
            return false;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBC.close(resultSet,null,ps,conn);
        }
        return false;
    }


    public boolean delete(int id){
        Connection conn = null;
        Statement statement = null;
        try{
            conn = JDBC.getConn();
            statement = JDBC.getStatement(conn);
            String sql = "delete from news where nid=" + id;
            int num = statement.executeUpdate(sql);
            if(num>0){
                return true;
            }
            return false;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBC.close(null,statement,null,conn);
        }
        return false;
    }

    public News find(int id){
        Connection conn = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try{
            conn = JDBC.getConn();
            statement = JDBC.getStatement(conn);
            String sql = "select * from news where nid =" + id;
            resultSet = statement.executeQuery(sql);
            while(resultSet.next()){
                News news = new News();
                news.setNtid(resultSet.getInt("Ntid"));
                news.setNtitle(resultSet.getString("ntitle"));
                news.setNauthor(resultSet.getString("nauthor"));
                news.setNcreateDate(resultSet.getString("ncreateDate"));
                news.setNpicPath(resultSet.getString("npicPath"));
                news.setNcontent(resultSet.getString("ncontent"));
                news.setNmodifyDate(resultSet.getString("nmodifyDate"));
                news.setNsummary(resultSet.getString("nsummary"));
                return news;
            }
            return null;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBC.close(resultSet,statement,null,conn);
        }
        return null;
    }

    public ArrayList<News> find_type(int type){
        Connection conn = null;
        Statement statement = null;
        ResultSet result = null;
        try{
            conn = JDBC.getConn();
            statement = JDBC.getStatement(conn);
            String sql = "select * from news where ntid="+type;
            ArrayList<News> list = new ArrayList<News>();
            result = JDBC.getResult(sql,statement);
            while(result.next()){
                News news = new News();
                news.setNid(result.getInt("nid"));
                news.setNtid(result.getInt("ntid"));
                news.setNtitle(result.getString("ntitle"));
                news.setNauthor(result.getString("nauthor"));

                news.setNcreateDate(result.getString("ncreateDate"));
                news.setNpicPath(result.getString("npicPath"));
                news.setNcontent(result.getString("ncontent"));
                news.setNmodifyDate(result.getString("nmodifyDate"));
                news.setNsummary(result.getString("nsummary"));
                list.add(news);
            }
            return list;

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBC.close(result,statement,null,conn);
        }
        return null;
    }

    /**
     * 通过id查询新闻的全部信息（包括评论）
     * @param id
     * @return
     */
    public ArrayList<News> findAll_comment(int id){
        Connection conn  = null;
        Statement statement = null;
        ResultSet result = null;
        try{
            conn = JDBC.getConn();
            statement = JDBC.getStatement(conn);
            String sql = "select * from news n,comments c where n.nid = c.cnid and n.nid =" + id;
            result = statement.executeQuery(sql);
            ArrayList<News> list = new ArrayList<News>();
            while(result.next()){
                News news = new News();
                news.setNid(result.getInt("nid"));
                news.setNtid(result.getInt("ntid"));
                news.setNtitle(result.getString("ntitle"));
                news.setNauthor(result.getString("nauthor"));

                news.setNcreateDate(result.getString("ncreateDate"));
                news.setNpicPath(result.getString("npicPath"));
                news.setNcontent(result.getString("ncontent"));
                news.setNmodifyDate(result.getString("nmodifyDate"));
                news.setNsummary(result.getString("nsummary"));
                news.setComments(result.getString("ccontent"));
                list.add(news);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBC.close(result,statement,null,conn);
        }
        return null;
    }
}
